package com.wy.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.wy.bean.Article;
import com.wy.service.ArticleService;
import com.wy.utils.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
public class ArticleController {

    @Resource
    private ArticleService articleService;

    @RequestMapping("list")
    public String list(Article article, Model model,
                       @RequestParam(defaultValue = "1")int pageNum,
                       @RequestParam(defaultValue = "3")int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Article> list = articleService.list(article);
        PageInfo<Article> pageInfo = new PageInfo<>(list);
        model.addAttribute("article",article);
        model.addAttribute("pageInfo",pageInfo);
        return "list";
    }

    /**
     * 进入新增页面
     */
    @RequestMapping("toadd")
    public String toadd(){
        return "add";
    }

    /**
     * 保存新增数据方法
     * mid=2,3
     */
    @RequestMapping("save")
    public String save(Article article, MultipartFile file){
        if(file != null && file.getSize()>0){//加上这个判断，图片不上传也可以保存成功
            String filename = file.getOriginalFilename();//获取图片名称123.jpg
            String substring = filename.substring(filename.lastIndexOf("."));//获取图片后缀.jpg
            String savename= UUID.randomUUID().toString()+substring;//为图片生成新的随机唯一名称
            try {
                file.transferTo(new File("D:\\myupload\\"+savename));
                article.setImg("/myupload/"+savename);//保存图片访问路径到实体类，插入到数据库
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //保存数据到数据库

        String content = article.getContent();
        //获取文件简介：内容截取前30个字符
        String jianjie ="";
        if(StringUtil.isNotEmpty(content) && content.length()>30){
            jianjie =content.substring(0, 30);
        } else{
            jianjie=content;
        }
        article.setJianjie(jianjie);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        article.setCreate_time(sdf.format(new Date()));
        articleService.insert(article);
        return "redirect:list.do";
    }

    /**
     * ajax批量删除
     */
    @RequestMapping("deleteBth")
    @ResponseBody
    public Boolean deleteBth(String[] ids){
        try {
            articleService.deletebth(ids);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

}
